package cn.lic.tool.poi.excel.function;

import com.alibaba.fastjson.JSONObject;

import cn.lic.tool.poi.excel.Utils;

public abstract class AbstractFunction implements IFunction {
	
	protected IFunctionArgument[] args;
	
	public IValue getArg(int index, JSONObject ctx) {
		if (args == null || index < 0 || args.length == 0 || index >= args.length) {
			return IValue.buildSValue(null);
		}
		IFunctionArgument arg = args[index];
		if (arg instanceof IFunction) {
			return  ((IFunction)arg).execute(ctx);
		} else if (arg instanceof IValue) {
			IValue val = (IValue)arg;
			val.setContext(ctx);
			return val;
		}
		throw new FunctionException("Unexpect value type: " + arg);
	}
	
	@Override
	public String toString() {
		return this.getClass().getSimpleName() + "(" + Utils.join(args) + ")";
	}
}
